package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.ErrorCode;
import cn.itsource.constants.RConst;
import cn.itsource.exception.GlobalException;
import cn.itsource.pojo.domain.CustomerCar;
import cn.itsource.mapper.CustomerCarMapper;
import cn.itsource.pojo.dto.CustomerDto;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ICustomerCarService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户车辆 服务实现类
 * </p>
 *
 * @author lifan
 * @since 2024-09-05
 */
@Service
public class CustomerCarServiceImpl extends ServiceImpl<CustomerCarMapper, CustomerCar> implements ICustomerCarService {

    @Override
    public Boolean addCar(CustomerDto customerDto) {
        // 查询该客户是否添加了该车辆
        CustomerCar one = this.getOne(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCarType, customerDto.getCarType())
                .eq(CustomerCar::getCarPlate, customerDto.getCarPlate())
                .eq(CustomerCar::getCustomerId, StpUtil.getLoginIdAsLong()));
        if (one == null){
            CustomerCar customerCar = new CustomerCar();
            customerCar.setCustomerId(StpUtil.getLoginIdAsLong());
            customerCar.setCarPlate(customerDto.getCarPlate());
            customerCar.setCarType(customerDto.getCarType());
            customerCar.setCreateTime(new Date());
            return super.save(customerCar);
        }else {
            throw new GlobalException(ErrorCode.ADD_CAR_FALSE);
        }
    }

    @Override
    public List<CustomerCar> getCar() {
        List<CustomerCar> list = super.list(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCustomerId, StpUtil.getLoginIdAsLong())
                .eq(CustomerCar::getDeleted, RConst.Num.BASE_LONG_0));
        return list;
    }
}
